package org.example.consumer;

import org.example.consumer.model.User;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

//描述：Consumer< T>接口接受一个T类型参数，没有返回值。
//@FunctionalInterface
//public interface Consumer<T> {
//    void accept(T t);
//}
public class ConsumerDemo {
    public static void myPrintStatic(String s){
        System.out.println(s);
    }
    public  void myPrintCommon(String s){
        System.out.println(s);
    }
    public static void main(String[] args)
    {
        User user = new User();
        Consumer<User> userConsumer = userParmeter -> userParmeter.setName("test");
        userConsumer.accept(user);
        System.out.println(user.getName());

        lambdaExpressionTest();
    }

    public static void lambdaExpressionTest()
    {
        List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");

        System.out.println("匿名类写法");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });

        System.out.println("lambda表达式写法");
        list.forEach(s -> System.out.println(s));

        System.out.println("::写法-1");
        list.forEach(ConsumerDemo::myPrintStatic);
        System.out.println("::写法-2");
        ConsumerDemo consumerDemo = new ConsumerDemo();
        list.forEach(consumerDemo::myPrintCommon);
    }
}
